﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class XemSuaPhongBan : Form
    {
        string MSPB;

        public XemSuaPhongBan()
        {
            InitializeComponent();
        }

        public XemSuaPhongBan(string MSPB)
        {
            InitializeComponent();
            this.MSPB = MSPB;
        }

        private void KhoaThongTin()
        {
            TenPB.Enabled = false;
        }

        private void XoaThongTin()
        {
            MaPB.Text = "";
            TenPB.Text = "";
        }

        private void loadDataGridView()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from PhongBan";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "PhongBan");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["PhongBan"];
        }

        private void loadThongTinPhongBan(string MSPB)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from PhongBan where MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_PB", MSPB);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                MaPB.Text = reader["MaSo_PB"].ToString();
                TenPB.Text = reader["Ten_PB"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void updatePhongBan(string MSPB, string TENPB)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update PhongBan set Ten_PB = @Ten_PB where MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_PB", MSPB);
            cmd.Parameters.AddWithValue("@Ten_PB", TENPB);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void deletePhongBan(string MSPB)
        {
            //Config file = new Config(Application.StartupPath + "\\config.ini");
            //string connectionstring = file.ReadValue("server", "con");
            //SqlConnection conn = new SqlConnection(connectionstring);
            //string sql = "delete from PhongBan where MaSo_PB = @MaSo_PB";
            ////string sql = "delete from PhongBan where MaSo_PB = @MaSo_PB";
            //SqlCommand cmd = new SqlCommand(sql, conn);
            //conn.Open();
            //cmd.Parameters.AddWithValue("@MaSo_PB", MSPB);
            //cmd.ExecuteNonQuery();
            //conn.Close();

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChucVu where MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_PB", MSPB);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                MessageBox.Show("Phòng ban còn tồn tại trong bảng Chức Vụ", "Thông báo", MessageBoxButtons.OK);
                XemSuaChucVu f = new XemSuaChucVu();
                f.ShowDialog();
                reader.Close();
            }
            else
            {
                reader.Close();
                cmd.CommandText = "delete from PhongBan where MaSo_PB = @MaSo_PB";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@MaSo_PB", MSPB);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }

        private void SuaPhongBan_Load(object sender, EventArgs e)
        {
            loadDataGridView();
        }

        private void QuayLaiButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CapNhatButton_Click(object sender, EventArgs e)
        {
            updatePhongBan(MaPB.Text, TenPB.Text);
            thongbaoLabel.Text = "Cập nhật thành công !";
            XoaThongTin();
            KhoaThongTin();
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string MSPB;
            MSPB= dataGridView1.Rows[e.RowIndex].Cells["MaSo_PB"].Value.ToString();
            loadThongTinPhongBan(MSPB);
        }

        private void ChinhSuaButton_Click(object sender, EventArgs e)
        {
            TenPB.Enabled = true;
        }

        private void QuayLaiButton_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void XoaButton_Click(object sender, EventArgs e)
        {
            ThongBaoXoa f = new ThongBaoXoa();
            if (f.ShowDialog() == DialogResult.OK)
            {
                deletePhongBan(MaPB.Text);
                XoaThongTin();
                loadDataGridView();
            }
            else
                f.Close();
        }

        private void MaPB_TextChanged(object sender, EventArgs e)
        {
            thongbaoLabel.Text = "";
        }
    }
}
